package com.timingbar.safe.library.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.util.Base64;

import java.io.*;

/**
 * DataHelper 数据保存工具类（SharedPreferences、sdcard）
 * -----------------------------------------------------------------------------------------------------------------------------------
 *
 * @author rqmei on 2018/1/29
 */
public class DataHelper {
    private static SharedPreferences mSharedPreferences;
    public static final String SP_NAME = "config";


    private DataHelper() {
    }

    /**
     * 存储重要信息到sharedPreferences；
     *
     * @param key
     * @param value
     */
    public static void setStringSF(Context context, String key, String value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit ().putString (key, value).apply ();
    }

    /**
     * 返回存在sharedPreferences的信息
     *
     * @param key
     * @return
     */
    public static String getStringSF(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString (key, null);
    }

    /**
     * 存储重要信息到sharedPreferences；
     *
     * @param key
     * @param value
     */
    public static void setIntergerSF(Context context, String key, int value) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit ().putInt (key, value).apply ();
    }

    /**
     * 返回存在sharedPreferences的信息
     *
     * @param key
     * @return
     */
    public static int getIntergerSF(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getInt (key, -1);
    }

    /**
     * 清除某个内容
     */
    public static void removeSF(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit ().remove (key).apply ();
    }

    /**
     * 清除Shareprefrence
     */
    public static void clearShareprefrence(Context context) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit ().clear ().apply ();
    }

    /**
     * 将对象储存到sharepreference
     *
     * @param key
     * @param device
     * @param <T>
     */
    public static <T> boolean saveDeviceData(Context context, String key, T device) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream ();
        try {   //Device为自定义类
            // 创建对象输出流，并封装字节流
            ObjectOutputStream oos = new ObjectOutputStream (baos);
            // 将对象写入字节流
            oos.writeObject (device);
            // 将字节流编码成base64的字符串
            String oAuth_Base64 = new String (Base64.encode (baos
                    .toByteArray (), Base64.DEFAULT));
            mSharedPreferences.edit ().putString (key, oAuth_Base64).apply ();
            return true;
        } catch (Exception e) {
            e.printStackTrace ();
            return false;
        }
    }

    /**
     * 将对象从shareprerence中取出来
     *
     * @param key
     * @param <T>
     * @return
     */
    public static <T> T getDeviceData(Context context, String key) {
        if (mSharedPreferences == null) {
            mSharedPreferences = context.getSharedPreferences (SP_NAME, Context.MODE_PRIVATE);
        }
        T device = null;
        String productBase64 = mSharedPreferences.getString (key, null);

        if (productBase64 == null) {
            return null;
        }
        // 读取字节
        byte[] base64 = Base64.decode (productBase64.getBytes (), Base64.DEFAULT);

        // 封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream (base64);
        try {
            // 再次封装
            ObjectInputStream bis = new ObjectInputStream (bais);

            // 读取对象
            device = (T) bis.readObject ();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
        return device;
    }

    /**
     * 返回缓存文件夹
     */
    public static File getCacheFile(Context context) {
        if (Environment.getExternalStorageState ().equals (Environment.MEDIA_MOUNTED)) {
            File file = null;
            file = context.getExternalCacheDir ();//获取系统管理的sd卡缓存文件
            if (file == null) {//如果获取的为空,就是用自己定义的缓存文件夹做缓存路径
                file = new File (getCacheFilePath (context));
                makeDirs (file);
            }
            return file;
        } else {
            return context.getCacheDir ();
        }
    }

    /**
     * 获取自定义缓存文件地址
     *
     * @param context
     * @return
     */
    public static String getCacheFilePath(Context context) {
        String packageName = context.getPackageName ();
        return "/mnt/sdcard/" + packageName;
    }


    /**
     * 创建未存在的文件夹
     *
     * @param file
     * @return
     */
    public static File makeDirs(File file) {
        if (!file.exists ()) {
            file.mkdirs ();
        }
        return file;
    }

    /**
     * 使用递归获取目录文件大小
     *
     * @param dir
     * @return
     */
    public static long getDirSize(File dir) {
        if (dir == null) {
            return 0;
        }
        if (!dir.isDirectory ()) {
            return 0;
        }
        long dirSize = 0;
        File[] files = dir.listFiles ();
        for (File file : files) {
            if (file.isFile ()) {
                dirSize += file.length ();
            } else if (file.isDirectory ()) {
                dirSize += file.length ();
                dirSize += getDirSize (file); // 递归调用继续统计
            }
        }
        return dirSize;
    }

    /**
     * 使用递归删除文件夹
     *
     * @param dir
     * @return
     */
    public static boolean deleteDir(File dir) {
        if (dir == null) {
            return false;
        }
        if (!dir.isDirectory ()) {
            return false;
        }
        File[] files = dir.listFiles ();
        for (File file : files) {
            if (file.isFile ()) {
                file.delete ();
            } else if (file.isDirectory ()) {
                deleteDir (file); // 递归调用继续删除
            }
        }
        return true;
    }


    public static String bytyToString(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream ();
        byte[] buf = new byte[1024];
        int num = 0;
        while ((num = in.read (buf)) != -1) {
            out.write (buf, 0, buf.length);
        }
        String result = out.toString ();
        out.close ();
        return result;
    }

    /**
     * 关闭IO
     *
     * @param closeables closeable
     */
    public static void closeIO(Closeable... closeables) {
        if (closeables == null)
            return;
        try {
            for (Closeable closeable : closeables) {
                if (closeable != null) {
                    closeable.close ();
                }
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

    /**
     * 获取mipmap下面的图片
     *
     * @param context 上下文
     * @param resId   资源图片id
     */
    public static String getMipmapImg(Context context, int resId) {
        return "android.resource://" + context.getApplicationContext ().getPackageName () + "/" + resId;
    }

    /**
     * 获取mipmap下面的图片
     *
     * @param fileName 在assets下的文件路径
     */
    public static String getAssetsImg(String fileName) {
        return "file:///android_asset/" + fileName;
    }
}
